from google.appengine.ext import db

class user(db.Model):
    roles       = db.StringListProperty()
    name        = db.StringProperty()
    password    = db.StringProperty()
    phone       = db.StringProperty()
    email       = db.StringProperty()
    '''
    def __init__(self, name1, pwd, phone=None, email=None):

        db.Model.__init__(self)
        self.name = name1
        self.password = pwd
        self.phone = phone
        self.email = email
    '''    
    def addRole(self, role):
        if isInRoles(role.name):
            return False
        else:
            roles.append(role)
            return True        
        pass
    def isInRoles(self, role_name):
        for x in roles: 
          if role_name == x.name:
            return True
        else:
          pass
        return False

    def removeRole(self, role):
        if isInRoles(role.name):
            roles.remove(role)
            return True
        else:
            return False
    
def addUser(new_user):
    if exist(new_user.name):
        return False
    new_user.put()
    return True

def exist(user_name):
    for x in users: 
      if user_name == x.name:
        return True
      else:
          continue
    return False
    
def removeUser(new_user):
    if exist(new_user.name):
        users.remove(new_user)
       
        return True
    else:
        return False

def usersToString():
    s = ''
    for r in users:
        if r.name != None:
            s+=(r.name + '; ')
    return s

users = db.GqlQuery('SELECT * FROM user')

